package com.blackteachan.server.response;

/**
 * 封装服务器响应
 * @author blackteachan
 * @since 2021-04-28 11:28
 */
public class ServerResponseUtil  {

    private static final int RESPONSE_CODE_SUCCESS = 200;
    private static final int RESPONSE_CODE_FAIL = 500;
    private static final int RESPONSE_CODE_UNAUTHORIZED = 401;
    private static final int RESPONSE_CODE_FORBIDDEN = 403;
    private static final String RESPONSE_MESSAGE_SUCCESS = "成功";
    private static final String RESPONSE_MESSAGE_FAIL = "失败";

    public static <T> ServerResponse<T> success(){
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setCode(RESPONSE_CODE_SUCCESS);
        serverResponse.setMsg(RESPONSE_MESSAGE_SUCCESS);
        return serverResponse;
    }

    public static <T> ServerResponse<T> success(String message){
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setCode(RESPONSE_CODE_SUCCESS);
        serverResponse.setMsg(message);
        return serverResponse;
    }

    public static <T> ServerResponse<T> success(T data){
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setCode(RESPONSE_CODE_SUCCESS);
        serverResponse.setData(data);
        serverResponse.setMsg(RESPONSE_MESSAGE_SUCCESS);
        return serverResponse;
    }

    public static <T> ServerResponse<T> success(String message, T data){
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setCode(RESPONSE_CODE_SUCCESS);
        serverResponse.setMsg(message);
        serverResponse.setData(data);
        return serverResponse;
    }

    public static <T> ServerResponse<T> fail(){
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setCode(RESPONSE_CODE_FAIL);
        serverResponse.setMsg(RESPONSE_MESSAGE_FAIL);
        return serverResponse;
    }

    public static <T> ServerResponse<T> fail(String message){
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setCode(RESPONSE_CODE_FAIL);
        serverResponse.setMsg(message);
        return serverResponse;
    }

    public static <T> ServerResponse<T> fail(T data){
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setCode(RESPONSE_CODE_FAIL);
        serverResponse.setData(data);
        serverResponse.setMsg(RESPONSE_MESSAGE_FAIL);
        return serverResponse;
    }

    public static <T> ServerResponse<T> fail(String message, T data){
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setCode(RESPONSE_CODE_FAIL);
        serverResponse.setMsg(message);
        serverResponse.setData(data);
        return serverResponse;
    }

    public static <T> ServerResponse<T> fail(Integer code, String message){
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setCode(code);
        serverResponse.setMsg(message);
        return serverResponse;
    }

    public static <T> ServerResponse<T> fail(Integer code, String message, T data){
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setCode(code);
        serverResponse.setMsg(message);
        serverResponse.setData(data);
        return serverResponse;
    }

    public static <T> ServerResponse<T> unauthorized(String message){
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setCode(RESPONSE_CODE_UNAUTHORIZED);
        serverResponse.setMsg(message);
        return serverResponse;
    }

    public static <T> ServerResponse<T> forbidden(String message){
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setCode(RESPONSE_CODE_FORBIDDEN);
        serverResponse.setMsg(message);
        return serverResponse;
    }

}
